package com.dgz.springboot18.controller;

import com.dgz.springboot18.pojo.AdminInfo;
import com.dgz.springboot18.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.Soundbank;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 靓仔登陆
     * @param adminInfo
     * @return
     */
    @PostMapping("/login")
    public Map<String, Object> login(AdminInfo adminInfo) {
        Map<String, Object> map = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();
        //将数据封装到token中
        UsernamePasswordToken token = new UsernamePasswordToken(adminInfo.getAdminName(), adminInfo.getAdminName() + adminInfo.getAdminPassword());
        try {
            subject.login(token);
            map.put("code", 0);
            map.put("msg", "success");
            map.put("data", subject.getPrincipal());
        } catch (UnknownAccountException e) {
            map.put("code", 100);
            map.put("msg", "账户不存在");
        } catch (IncorrectCredentialsException e) {
            map.put("code", 100);
            map.put("msg", "密码错误");
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "未知错误");
        }
        return map;
    }




}
